package com.fmatos.amazingwallpapers.dagger;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import android.util.Log;
import dagger.ObjectGraph;

public class AmazingApplication extends Application {
	private static final String TAG = AmazingApplication.class.getSimpleName();

	private ObjectGraph graph;

	private Activity currentActivity;

	@Override public void onCreate() {
		super.onCreate();

		graph = ObjectGraph.create(getModules());
	}

	protected Object[] getModules() {
		List<DaggerMasterModule> list = Arrays.asList(
				new DaggerMasterModule(this)
				);
		
		return list.toArray();
	}

	/*
	 * Used to inject objects from within an activity, 
	 * while registering the related activity 
	 * so it can be injected on objects farther away in the graph 
	 */
	public void inject(Fragment object,Activity activity) {

		if ( activity != null  ) {
			this.currentActivity = activity;
		}
		Log.d(TAG,"Injecting " + object.getClass().getSimpleName() + "  / activity = " + activity);
		graph.inject(object);
	}

	/**
	 * Returns the current activity, if dagger was injected from an activity using 
	 * {@link #inject(Fragment, Activity) method. Returns null otherwise}
	 * @return
	 */
	public Activity getCurrentActivity() {
		return currentActivity;
	}

	public void inject(Object object) {

		Log.d(TAG,"Injecting " + object.getClass().getSimpleName());
		graph.inject(object);
		
	}
}